package com.neusoft.ecommerce.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.neusoft.ecommerce.dbhelper.LayuiPager;
import com.neusoft.ecommerce.entity.Indent;
import com.neusoft.ecommerce.entity.Payment;
import com.neusoft.ecommerce.service.IndentService;
import com.neusoft.ecommerce.service.PaymentService;

/**
 * 
 * @author Alouette~
 *
 */
@RestController
public class PaymentController {
	private final PaymentService paymentService;
	private final IndentService indentService;
	
	public PaymentController(PaymentService paymentService, IndentService indentService) {
		this.paymentService = paymentService;
		this.indentService = indentService;
	}
	
	@GetMapping("/payment_page")
    public ModelAndView paymentPage() {
        return new ModelAndView("payment/payment-list");
    }
	
    @RequestMapping("/payment/list")
    @ResponseBody
    public LayuiPager<Payment> list(@RequestParam(value = "page", defaultValue = "1") String page,
                                    @RequestParam(value = "limit", defaultValue = "10") String limit) {
        System.out.println(page + "" + limit);
        LayuiPager<Payment> pager = new LayuiPager<>(Integer.parseInt(page), Integer.parseInt(limit));
        int count = paymentService.findTotal(pager);
        List<Payment> list = paymentService.findList(pager);
        for (Payment payment : list) {
            System.out.println(payment);
        }
        pager.setData(list);
        pager.setCount(count);
        pager.setCode(0);
        pager.setMsg("");
        return pager;
    }
	
	@GetMapping("/addPayment")
    public ModelAndView addPayment() {
        return new ModelAndView("payment/payment-add");
    }
	
	@RequestMapping("/payment/indentList")
    public List<Indent> findIndentList() {
	    return indentService.findAll();
    }
	
	@PostMapping("/payment/add")
    public Map<String, Object> add(Payment payment) {
        System.out.println(payment);
        Map<String, Object> map = new HashMap<>(16);
        if (paymentService.add(payment)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }
	
	@GetMapping("/payment/edit_page")
    public ModelAndView editPage(Payment payment, Model model) {
        System.out.println(payment);
        LayuiPager<Payment> pager = new LayuiPager<>(1,1);
        pager.setCondition(payment);
        Payment message = paymentService.findList(pager).get(0);
        System.out.println(message);
        ModelAndView view = new ModelAndView("payment/payment-edit");
        model.addAttribute("payment", message);
        return view;
    }
	

	@PostMapping("/payment/edit")
    public Map<String, Object> edit(Payment payment) {
        System.out.println(payment);
        Map<String, Object> map = new HashMap<>(16);
        if (paymentService.edit(payment)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }
	@PostMapping("/payment/delete")
    public Map<String, Object> delete(Payment payment) {
        System.out.println(payment);
        Map<String, Object> map = new HashMap<>(16);
        if (paymentService.delete(payment)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }
}
